package com.fordmotor.util;

import java.lang.reflect.Field;
import java.util.*;

public class MapUtil {
    public static String valueSortedMap(TreeMap treeMap){
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(treeMap.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
            //降序排序
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue()-o1.getValue();
            }
        });
       return list.get(0).getKey();
    }

    public static Map<String, Integer> objectToMap(Object obj)  {
        if(obj == null){
            return null;
        }
        Map<String, Integer> map = null;
        try {
            map = new HashMap<String, Integer>();
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.getInt(obj));
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

}
